package com.hqyj.system.controller;

import com.hqyj.system.model.SysPermission;
import com.hqyj.system.model.SysRole;
import com.hqyj.system.model.SysRolePermission;
import com.hqyj.utils.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Auther: ZLF
 * @Date: 2021/12/30 10:54
 * @Description:
 */
@Controller
@RequestMapping("/sysPermissionController")
public class SysPermissionController extends BaseController {
    @RequestMapping("/list")
    public String list(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysPermission sysPermission) throws IOException {
        //1.接收数据
        System.err.println("-----SysPermissionController-list---");
        //3.调用业务
        List<SysPermission> sysPermissionList_db = sysPermissionService.queryAllSysPermission();
        model.addAttribute("sysPermissionList_db", sysPermissionList_db);
        //2.跳转页面
        return "view/system/sysPermission/sysPermissionList";
    }

    @RequestMapping("/addUI")
    public String addUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysPermission sysPermission) throws IOException {
        //1.接收数据
        //3.调用业务
        //准备父级节点数据
        List<SysPermission> sysPermissionList_db = sysPermissionService.queryAllSysPermission();
        model.addAttribute("sysPermissionList_db", sysPermissionList_db);
        //2.跳转页面
        return "view/system/sysPermission/addSysPermission";
    }

    @RequestMapping("/add")
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysPermission sysPermission, String pIdSTR) throws IOException {
        //1.接收数据
        //3.调用业务
        //维护数据
        int pId = Integer.parseInt(pIdSTR);
        sysPermission.setpId(pId);
        int n = sysPermissionService.addSysPermissionBySysPermission(sysPermission);
        //2.跳转页面
        List<SysPermission> sysPermissionList_db = sysPermissionService.queryAllSysPermission();
        model.addAttribute("sysPermissionList_db", sysPermissionList_db);
        return "view/system/sysPermission/sysPermissionList";
    }

    @RequestMapping("/delete")
    public String delete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysPermission sysPermission, String permissionIdStr) throws IOException {
        //1.接收数据
        //3.调用业务
        //字符类型转数字类型
        int permissionId = Integer.parseInt(permissionIdStr);
        int n = sysPermissionService.deleteSysPermissionByPermissionId(permissionId);

        //2.跳转页面
        List<SysPermission> sysPermissionList_db = sysPermissionService.queryAllSysPermission();
        model.addAttribute("sysPermissionList_db", sysPermissionList_db);
        return "view/system/sysPermission/sysPermissionList";
    }


    @RequestMapping("/updateUI")
    public String updateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysPermission sysPermission, String permissionIdStr) throws IOException {
        //1.接收数据

        //字符类型转数字类型
        int permissionId = Integer.parseInt(permissionIdStr);
        //3.调用业务
        //a.准备被修改数据
        SysPermission sysPermission_db = sysPermissionService.querySysPermissionByPermissionId(permissionId);
        model.addAttribute("sysPermission_db", sysPermission_db);
        //b.准备父级数据
        List<SysPermission> sysPermissionList_db = sysPermissionService.queryAllSysPermission();
        model.addAttribute("sysPermissionList_db", sysPermissionList_db);


        //2.跳转页面
        return "view/system/sysPermission/updateSysPermission";
    }

    @RequestMapping("/update")
    public String update(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysPermission sysPermission, String permissionIdSTR, String pIdSTR) throws IOException {
        //1.接收数据
        //字符类型转数字类型
        int pId = Integer.parseInt(pIdSTR);
        int permissionId = Integer.parseInt(permissionIdSTR);
        //3.调用业务
        sysPermission.setpId(pId);
        sysPermission.setPermissionId(permissionId);
        int n = sysPermissionService.updateSysPermissionBySysPermission(sysPermission);
        //2.跳转页面
        List<SysPermission> sysPermissionList_db = sysPermissionService.queryAllSysPermission();
        model.addAttribute("sysPermissionList_db", sysPermissionList_db);
        return "view/system/sysPermission/sysPermissionList";
    }
    @RequestMapping("/FPPermissionUI")
    public String FPPermissionUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysPermission sysPermission, String permissionIdSTR, String pIdSTR, String roleIdStr) throws IOException {
        //1.接收数据
        //字符类型转数字类型
        int roleId = Integer.parseInt(roleIdStr);
        System.err.println(roleId);
        //3.调用业务
        //a.角色数据
        SysRole sysRole_db = sysRoleService.querySysRoleByRoleId(roleId);
        model.addAttribute("sysRole_db", sysRole_db);
        //b.权限
        List<SysPermission> sysPermissionList_db = sysPermissionService.queryAllSysPermission();
        model.addAttribute("sysPermissionList_db", sysPermissionList_db);

        //2.跳转页面
        return "view/system/sysPermission/FPPermissionUI";
    }

    @RequestMapping("/FPPermission")
    public String FPPermission(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, SysRolePermission sysRolePermission, SysPermission sysPermission, String permissionIdSTR, String pIdSTR, String roleIdStr, String[] permissionIdStrs) throws IOException {
        //1.接收数据
        int roleId = Integer.parseInt(roleIdStr);

        //---->先删除
        int m = sysRolePermissionService.deleteSysRolePermissionByRoleId(roleId);

        for (String permissionIdStr : permissionIdStrs) {
            int permissionId = Integer.parseInt(permissionIdStr);
            //3.调用业务
            sysRolePermission.setRoleId(roleId);
            sysRolePermission.setPermissionId(permissionId);
            int n = sysRolePermissionService.addSysRolePermissionBySysRolePermission(sysRolePermission);
        }

        //2.跳转页面
        //a.角色数据
        SysRole sysRole_db = sysRoleService.querySysRoleByRoleId(roleId);
        model.addAttribute("sysRole_db", sysRole_db);
        //b.权限
        List<SysPermission> sysPermissionList_db = sysPermissionService.queryAllSysPermission();
        model.addAttribute("sysPermissionList_db", sysPermissionList_db);
        return "view/system/sysPermission/FPPermissionUI";
    }
}